<?php
namespace app\admin\controller;
use app\admin\logic\WorkRecordAccountTotalLogic;

/**
 * 日记账统计api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/21
 * Time: 11:07
 */
class WorkRecordAccountTotal extends Base {

    public function __construct(){
        parent::__construct();
        $this->wrat_logic = new WorkRecordAccountTotalLogic();
    }

    /**
     * 获取日记账统计
     * @return \think\response\Json
     */
    public function ajax_get_wrat(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取日记账统计
        $r = $this->wrat_logic->get_wrat($params);

        $data['list'] = $r['data']['list'];
        return json($this->wrat_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出日记账统计
     * http://127.0.0.1/mandiao-mini-api/public/admin-work_record_account_total-export_wrat?token=e916a6039bff505bacaf5def2bb8435f&department=全部&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_wrat(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取日记账统计
        $r = $this->wrat_logic->get_wrat($params);
        $list = $r['data']['list'];

        $row = ['部门名称','收入金额（元）','支出金额（元）'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['income_money'] = $v['income_money'];
            $export_data[$k]['expend_money'] = $v['expend_money'];
        }
        array_unshift($export_data,$row);

        $file_name = '日记账统计' . date('YmdHi') . '.xlsx';
        $r = $this->wrat_logic->gen_wrat_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->wrat_logic->get_response(1,'导出成功'));
    }
}